define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop_withdrawals/index' + location.search,
                    add_url: 'shop_withdrawals/add',
                    edit_url: 'shop_withdrawals/edit',
                    del_url: 'shop_withdrawals/del',
                    multi_url: 'shop_withdrawals/multi',
                    import_url: 'shop_withdrawals/import',
                    table: 'shop_withdrawals',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: false,
                search:false,
                showColumns:false,
                showExport:false,
                commonSearch:false,
                escape: false,
                showToggle: false,
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'bank_name', title: __('Bank_name'), operate: 'LIKE'},
                        {field: 'bank_account_name', title: __('Bank_account_name'), operate: 'LIKE'},
                        {field: 'bank_account', title: __('Bank_account'), operate: 'LIKE'},
                        {field: 'status_text', title: __('Status'), operate: 'LIKE'},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE'},
                        {field: 'audittime', title: __('Audittime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'shop_withdrawals/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'shop_withdrawals/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'shop_withdrawals/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        flowing_water: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop_withdrawals/flowing_water' + location.search,
                    add_url: 'shop_withdrawals/add',
                    edit_url: 'shop_withdrawals/edit',
                    // del_url: 'shop_withdrawals/del',
                    multi_url: 'shop_withdrawals/multi',
                    import_url: 'shop_withdrawals/import',
                    table: 'order',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                // showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                maintainSelected: true,
                columns: [
                    [
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'nickname', title: '購買用戶', operate: false},
                        {field: 'mobile', title: '用戶手機號碼', operate: false},
                        {field: 'pay_price', title: '金額變動', operate:false},
                        {field: 'order_status_texts', title: '狀態', operate:false},
                        {field: 'paytime', title: '支付時間', operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'receipttime', title: '完成時間', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime}
                    ]
                ]
            });

            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".btn-success", function () {
                var params = $("form").serializeArray();
                var params = JSON.stringify (params);
                Controller.total_price(params);
            });
            $(document).on("click", ".btn-default", function () {
                var params = $("form").serializeArray();
                var params = JSON.stringify (params);
                Controller.total_price(params);
            });
            
        },
        // 查詢合計金額
        total_price:function(data){
            $.ajax({
                url: 'shop_withdrawals/flowing_water',
                type: 'post',
                data: data,
                success: function (res) {
                    $("#total_price").html('合計金額：HK$ '+res.total_price);
                }
            });
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"), function(data, ret){
               //给表单绑定新的回调函数 接收 控制器 success(msg,url,data)或者error(msg,url,data)
                Fast.api.close(data);//在这里
                console.log("成功");
                // window.location.reload(); 
                parent.location.reload(); 
            }, function(data, ret){
               console.error("错误");
            });
            // Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});